package com.yhn.bubblevideo.admin.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yhn.bubblevideo.admin.mapper.CollectGroupMapper;
import com.yhn.bubblevideo.admin.service.CollectGroupService;
import com.yhn.bubblevideo.common.enums.BaseErrorCode;
import com.yhn.bubblevideo.common.exception.BusinessException;
import com.yhn.bubblevideo.common.exception.ThrowUtils;
import com.yhn.bubblevideo.model.domain.CollectGroup;
import com.yhn.bubblevideo.model.dto.req.collectgroup.CollectGroupAddRequestDTO;
import com.yhn.bubblevideo.model.dto.req.collectgroup.CollectGroupEditRequestDTO;
import com.yhn.bubblevideo.model.dto.req.collectgroup.CollectGroupQueryRequestDTO;
import com.yhn.bubblevideo.model.dto.req.common.DeleteRequest;
import com.yhn.bubblevideo.model.dto.resp.CollectGroupRespDTO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.yhn.bubblevideo.common.constant.CommonConstant.MAX_PAGE_SIZE;

/**
 * @author yhnyhn123
 * @description 针对表【collectGroup_gruop(收藏夹表)】的数据库操作Service实现
 * @createDate 2024-11-21 13:33:34
 */
@Service
public class CollectGroupServiceImpl extends ServiceImpl<CollectGroupMapper, CollectGroup>
        implements CollectGroupService {


    /**
     * 分页查询
     */
    @Override
    public Page<CollectGroupRespDTO> queryPage(CollectGroupQueryRequestDTO requestParam) {
        if (requestParam == null) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        long current = requestParam.getCurrent();
        long size = requestParam.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > MAX_PAGE_SIZE, BaseErrorCode.PARAMS_ERROR);
        Page<CollectGroup> collectGroupPage = this.page(new Page<>(current, size),
                this.getQueryWrapper(requestParam));
        Page<CollectGroupRespDTO> collectGroupRespPage = new Page<>(current, size, collectGroupPage.getTotal());
        List<CollectGroupRespDTO> collectGroupVO = this.getCollectGroupVO(collectGroupPage.getRecords());
        collectGroupRespPage.setRecords(collectGroupVO);
        return collectGroupRespPage;
    }

    @Override
    public LambdaQueryWrapper<CollectGroup> getQueryWrapper(CollectGroupQueryRequestDTO requestParam) {

        if (requestParam == null) {
            return null;
        }

        LambdaQueryWrapper<CollectGroup> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper
//                .like(requestParam.getCollectGroupName() != null, CollectGroup::get, requestParam.getName());
        return lambdaQueryWrapper;
    }

    /**
     * 获取脱敏对象集合
     *
     * @param collectGroupList 查询请求
     * @return 脱敏对象集合
     */
    @Override
    public List<CollectGroupRespDTO> getCollectGroupVO(List<CollectGroup> collectGroupList) {
        if (CollUtil.isEmpty(collectGroupList)) {
            return new ArrayList<>();
        }
        return collectGroupList.stream().map(this::getCollectGroupVO).collect(Collectors.toList());
    }


    /**
     * 将对象转化为脱敏对象
     *
     * @param collectGroup 原对象
     * @return 脱敏对象
     */
    @Override
    public CollectGroupRespDTO getCollectGroupVO(CollectGroup collectGroup) {
        if (collectGroup == null) {
            return null;
        }
        CollectGroupRespDTO collectGroupResp = new CollectGroupRespDTO();
        BeanUtils.copyProperties(collectGroup, collectGroupResp);
        return collectGroupResp;
    }

    /**
     * 根据ID获取信息
     *
     * @param id id
     * @return 实体信息
     */
    @Override
    public CollectGroupRespDTO getInfoById(Long id) {
        CollectGroup collectGroup = baseMapper.selectById(id);
        if (collectGroup == null) {
            throw new BusinessException(BaseErrorCode.NOT_FOUND_ERROR, "没有对应的信息");
        }
        return getCollectGroupVO(collectGroup);
    }

    /**
     * 保存信息
     *
     * @param requestParam 添加请求
     * @return 新生成的ID
     */
    @Override
    @Transactional
    public Long saveCollectGroup(CollectGroupAddRequestDTO requestParam) {
        if (requestParam == null) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        LambdaQueryWrapper<CollectGroup> queryWrapper = Wrappers.lambdaQuery(CollectGroup.class)

                .eq(CollectGroup::getIsDelete, 0);
        baseMapper.selectCount(queryWrapper);
        if (baseMapper.selectCount(queryWrapper) > 0) {
            throw new BusinessException("该题目已存在");
        }
        CollectGroup collectGroup = new CollectGroup();
        BeanUtils.copyProperties(requestParam, collectGroup);
        boolean result = this.save(collectGroup);
        ThrowUtils.throwIf(!result, BaseErrorCode.OPERATION_ERROR);
        return collectGroup.getId();
    }


    /**
     * 修改信息
     *
     * @param requestParam 修改请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean updateCollectGroup(CollectGroupEditRequestDTO requestParam) {
        if (requestParam == null || requestParam.getId() <= 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        CollectGroup collectGroup = new CollectGroup();
        BeanUtils.copyProperties(requestParam, collectGroup);
        long id = requestParam.getId();
        // 判断是否存在
        CollectGroup oldCollectGroup = this.getById(id);
        ThrowUtils.throwIf(oldCollectGroup == null, BaseErrorCode.NOT_FOUND_ERROR);
        boolean result = this.updateById(collectGroup);
        ThrowUtils.throwIf(!result, BaseErrorCode.OPERATION_ERROR);
        return true;
    }

    /**
     * 删除信息
     *
     * @param deleteRequest 删除请求
     * @return 是否修改成功
     */
    @Override
    @Transactional
    public Boolean deleteById(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        boolean b = this.removeById(id);
        ThrowUtils.throwIf(!b, BaseErrorCode.NOT_FOUND_ERROR);
        return true;
    }


    /**
     * 批量删除
     *
     * @param ids 批量删除的IDS
     * @return 是否删除成功
     */
    @Override
    @Transactional
    public Boolean deleteBatchByIds(Long[] ids) {
        if (ids == null || ids.length == 0) {
            throw new BusinessException(BaseErrorCode.PARAMS_ERROR);
        }
        boolean b = this.removeByIds(Arrays.asList(ids));
        ThrowUtils.throwIf(!b, BaseErrorCode.NOT_FOUND_ERROR);
        return true;
    }
}




